package com.songcha.library_base.mvvm.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowInsets
import android.view.WindowManager
import android.widget.LinearLayout
import androidx.core.view.OnApplyWindowInsetsListener
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.songcha.library_base.base.BaseActivity
import com.songcha.library_common.util.StatusBarUtil
import java.lang.reflect.ParameterizedType

abstract class BaseMvvmActivity<VM:BaseViewModel<out BaseRepository>,DB:ViewDataBinding>: BaseActivity() ,IMvvm{

     lateinit var viewModel: VM
     lateinit var databinding: DB

     lateinit var abc: String


     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
     }

    fun isViewModelInitialized()=::viewModel.isInitialized

    override fun initOnCreate(savedInstanceState: Bundle?) {
        super.initStatusBar(savedInstanceState)

        //get genericity type
        val cls=(this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM>
        viewModel=ViewModelProvider(this).get(cls)
        if(mRootView==null) return
        databinding=DataBindingUtil.bind(mRootView!!)!!

        initView(savedInstanceState)
        viewModel.isInit=true

        //setOnApplyWindowInsetsListener()

        initObserver()

        if(isInitViewGetData())
            onGetData()
    }


    //if getdata on resume(),then no need to getdata on initview()
    protected open fun isInitViewGetData():Boolean{
        return true
    }


    //need to get rootview and bind for databinding
    /*override fun initStatusBar(savedInstanceState: Bundle?){
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        StatusBarUtil.setTransparentStatusBar(this)
        StatusBarUtil.setStatusBarStyle(this,isDarkStatusBar())

        mRootView=getRootView()?:LayoutInflater.from(this).inflate(getLayoutId(),null)
        if(!isImmerseStatusBar()){
            val linear= LinearLayout(this)
            linear.orientation= LinearLayout.VERTICAL
            val statusBarView= View(this)
            statusBarView.setBackgroundColor(getStatusBarColor())
            linear.addView(statusBarView, LinearLayout.LayoutParams.MATCH_PARENT, StatusBarUtil.getStatusBarHeight(this))
            linear.addView(mRootView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
            mRootView=super.initLoad(linear)
            setContentView(mRootView)
        }else{
            mRootView=super.initLoad(mRootView!!)
            setContentView(mRootView)
        }
    }*/

    override fun onRetry() {
        onGetData()
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        viewModel.state.observe(this){
            when(it){
                BaseViewModel.LoadState.LOADING->onLoading()
                BaseViewModel.LoadState.LOAD_SUCCESS->onLoadSuccess()
                BaseViewModel.LoadState.LOAD_ERROR->onLoadError(viewModel.error!!)
                BaseViewModel.LoadState.LOAD_NO_NETWORK->onLoadNoNetwork()
                BaseViewModel.LoadState.LOAD_CONNECT_ERROR->onLoadConnectError()
                BaseViewModel.LoadState.LOAD_NO_DATA->onLoadNoData()
                BaseViewModel.LoadState.LOAD_TIMEOUT->onLoadTimeOut()

            }

            viewModel.dialogState.observe(this){
                if(it){
                    showLoadingDialog()
                }else{
                    hideLoadingDialog()
                }
            }
        }

    }

    protected open fun onGetData(){}

    protected open fun initObserver(){}

 }